package com.wcfsy.wechat.wx.util.excel;

import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ExcelReader {
    private Workbook wb;
    private Sheet sheet;
    private Row row;
    public ExcelReader(InputStream in){
        try {
            if (!in.markSupported()) {
                in = new PushbackInputStream(in, 8);
            }
            if (POIFSFileSystem.hasPOIFSHeader(in)) {
                wb = new HSSFWorkbook(in);
            }
            if (wb == null && POIXMLDocument.hasOOXMLHeader(in)) {
                wb = new XSSFWorkbook(OPCPackage.open(in));
            }
        } catch (Exception e) {
           
            throw new RuntimeException("您上传的excel不能解析，请下载模板导入数据!");
        }
        sheet = wb.getSheetAt(0);
    }
    public Object[] readExcelTitle() {
        row = sheet.getRow(0);
        if(row==null) return null;
        int colNum = row.getPhysicalNumberOfCells();
        Object[] objs = new Object[colNum+1];
        for(int j =0;j<=colNum;j++){
            objs[j] = getCellValue(row.getCell(j));
        }
        return objs;
    }
    
    public List<Object[]> readExcelContent(){
        
        // 得到总行数
        int rowNum = sheet.getLastRowNum();
        row = sheet.getRow(0);
        List<Object[]> list = null;
        for (int i = 1; i <= rowNum; i++){
            row = sheet.getRow(i);
            int colNum = row.getPhysicalNumberOfCells();
            if(list==null) list = new ArrayList<Object[]>();
            Object[] objs = new Object[colNum+1];
            getCellValue(row.getCell(colNum));
            for(int j =0;j<=colNum;j++){
                objs[j] = getCellValue(row.getCell(j));
            }
            
            list.add(objs);
        }
        return list;
    }

    private Object getCellValue(Cell cell){
        Object obj = null;
        if (cell != null) {
            switch (cell.getCellType()){
            case HSSFCell.CELL_TYPE_NUMERIC:
                obj = cell.getNumericCellValue();
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                obj = cell.getDateCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                obj = cell.getBooleanCellValue();
                break;
            case HSSFCell.CELL_TYPE_STRING:
                obj = cell.getStringCellValue();
                break;
            default :
                obj = null;
            }
        }
        return obj;
    }
    
}
